﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Caching;


namespace ArtsShop.BusinessLogic
{
    public class Settings
    {
        #region Private Properties
        private static readonly Settings _singleton = new Settings();
        private Dictionary<string, string> _dict;

        #endregion

        #region Singleton
        private Settings()
        {
        }

        public static Settings Instance
        {
            get
            {
                return new Settings();
            }
        }
        #endregion

        #region Dictionary
        protected Dictionary<string, string> Dictionary
        {
            get
            {
                if (_dict == null)
                {
                    string cacheKey = "WebSiteConfig";
                    //_dict = (Dictionary<string, string>)Cache.GetCachedObject(cacheKey);

                    if (_dict == null)
                    {
                        _dict = CreateDictionary();
                       /* if (ConfigurationManager.AppSettings[CachePublicKeys.WEBSITE_CONFIGURATION] != null)
                        {
                            Cache.AddToCache(cacheKey, _dict, new TimeSpan(1, 0, 0), CachePublicKeys.WEBSITE_CONFIGURATION);
                        }*/
                    }
                }

                return _dict;
            }
        }
        #endregion

        #region Public Methods

        #region Get
        public T Get<T>(string key)
        {
            T result = default(T);
            key = key.ToLower();
            if (Dictionary.ContainsKey(key))
            {
                string value = Dictionary[key];

                if (!string.IsNullOrEmpty(value))//(!CastingHelper.IsNullOrEmpty(value))
                {
//s                    result = (T)Utility.ChangeType(value, typeof(T));
                    return result;
                }
                else
                {
                   // Buy4NowException ex = new Buy4NowException(string.Format("Key [{0}] missing in the database.", key));
                    //ex.Category = "Settings";
                    //throw (ex);
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
        #endregion

        public T GetSafe<T>(string key)
        {
            T result = default(T);
            key = key.ToLower();
            try
            {
                if (!Dictionary.ContainsKey(key))
                {
                    return default(T);
                }
                string value = Dictionary[key];

                if (!string.IsNullOrEmpty(value))//(!CastingHelper.IsNullOrEmpty(value))
                {
                    //result = (T)Utility.ChangeType(value, typeof(T));
                    return result;
                }
                else
                {
                    return default(T);
                }
            }
            catch
            {
                return default(T);
            }

        }

        #endregion

        #region Private Methods

        #region CreateDictionary
        private Dictionary<string, string> CreateDictionary()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

          /*  B4NDatabase.StoredProcedure configuration = DataBase.Site.BuildStoredProc("[dbo].[Config.GetAllSettings]");
            DataSet ds = configuration.ExecuteDataSet();

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (!row.IsNull("idname") && !row.IsNull("idvalue"))
                {
                    dict.Add((string)row["idname"].ToString().ToLower(), (string)row["idvalue"]);
                }
            }
            */
            return dict;
        }
        #endregion

        #endregion
    }
}
